Learning Flex Basics (Part 2): Creating a Simple Calculator with Macromedia Flex
Table of Contents
- Introduction
- Building the Calculator
- Create the Application Object
- Add a Panel for the Calculator
- Add the Calculator Display
- Add a Grid to Lay Out the Buttons
- Add the Calculator Buttons
- Create the CalculatorController Class
- Add Properties to the Controller Class
- Add Controller Methods
- Instantiate the CalculatorController Class
- Complete Code Listing for calculator.mxml
- Complete Code Listing for CalculatorController.as
- Where to Go From Here
Instantiate the CalculatorController Class
The final step of the tutorial is to instantiate the CalculatorController
class you just created in the calculator.mxml file. You can do this through
a tag, but since the tag will not be part of the Flex class library, you
will have to add a new namespace for it. You do this by adding an additional xmlns property
to the Application tag, giving it a value of "*".
The "*" value simply indicates: "accept any class found
in the application directory as an element."
Also remember that the CalculatorController class needs a reference to the
calculator Application object so that it can access the CalcDisplay object.
You pass this reference by setting the calcView property of the
CalculatorController object to {this}; in an MXML file, this is
a reference to the overall Application object. The curly braces create data
binding, so that the Flex compiler evaluates the contents as an expression
rather that treating it as a literal string.
Finally, CalculatorController object needs an instance name so that other
elements in the calculator can refer to it; the instance name is the id attribute
of the tag that instantiates the class.
-
Return to calculator.mxml and add an additional
xmlnsproperty to theApplicationtag; give the value of the namespace the value of "*" and do not assign a prefix to it:<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
-
At the beginning of the
Applicationtag block, add a CalculatorController tag with the attributesid="calcController"andcalcView="{this}":<CalculatorController id="calcController" calcView="{this}"/> - Save the file and test it in a web browser (for information on how to browse MXML files, see the first page in this tutorial).